import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import smCrypto from '@/utils/sm-crypto'
import loginApi from '@/api/auth/login-api'
import userCenterApi from '@/api/sys/user-center-api'
import { getToken, setToken, removeToken } from '@/utils/auth'
import dictApi from '@/api/dev/dict-api'
import env from '@/env'
export default {
	state: {
		// 环境key
		envKey: storage.get(constant.envKey) || env.DEFAULT_ENV_KEY,
		// 所有环境
		allEnv: storage.get(constant.allEnv) || env.DEFAULT_ALL_ENV,
		// token信息
		token: getToken(),
		// 首页配置
		homeConfigs: storage.get(constant.homeConfigs) || config.HOME_CONFIGS,
		// 用户移动端菜单（用户菜单处理后的结果）
		userMobileMenus: storage.get(constant.userMobileMenus),
		// 用户信息
		userInfo: storage.get(constant.userInfo),
		// 系统配置
		sysBaseConfig: storage.get(constant.sysBaseConfig) || config.SYS_BASE_CONFIG,

		// 刷新KEY
		refreshKey: storage.get(constant.refreshKey) || 'page',
		// 刷新标记
		refreshFlag: storage.get(constant.refreshFlag) || false,
		// 刷新参数
		refreshParam: storage.get(constant.refreshParam) || null
	},
	mutations: {
		SET_envKey: (state, envKey) => {
			state.envKey = envKey
			storage.set(constant.envKey, envKey)
		},
		SET_allEnv: (state, allEnv) => {
			state.allEnv = allEnv
			storage.set(constant.allEnv, allEnv)
		},
		
		SET_token: (state, token) => {
			state.token = token
			setToken(token)
		},
		SET_homeConfigs: (state, homeConfigs) => {
			state.homeConfigs = homeConfigs
			storage.set(constant.homeConfigs, homeConfigs)
		},
		SET_userMobileMenus: (state, userMobileMenus) => {
			state.userMobileMenus = userMobileMenus
			storage.set(constant.userMobileMenus, userMobileMenus)
		},
		SET_userInfo: (state, userInfo) => {
			state.userInfo = userInfo
			storage.set(constant.userInfo, userInfo)
		},
		SET_sysBaseConfig: (state, sysBaseConfig) => {
			state.sysBaseConfig = sysBaseConfig
			storage.set(constant.sysBaseConfig, sysBaseConfig)
		},

		SET_refreshKey: (state, refreshKey) => {
			state.refreshKey = refreshKey
			storage.set(constant.refreshKey, refreshKey)
		},
		SET_refreshFlag: (state, refreshFlag) => {
			state.refreshFlag = refreshFlag
			storage.set(constant.refreshFlag, refreshFlag)
		},
		SET_refreshParam: (state, refreshParam) => {
			state.refreshParam = refreshParam
			storage.set(constant.refreshParam, refreshParam)
		},
		// 清除缓存
		CLEAR_cache: (state) => {
			state.token = ''
			removeToken()
			// 移动端用户菜单
			state.userMobileMenus = {}
			storage.remove(constant.userMobileMenus)
			// 用户信息
			state.userInfo = {}
			storage.remove(constant.userInfo)
			// 刷新标识
			state.refreshKey = null
			storage.remove(constant.refreshKey)
			// 刷新标识
			state.refreshFlag = false
			storage.remove(constant.refreshFlag)
			// 刷新参数
			state.refreshParam = null
			storage.remove(constant.refreshParam)
			// 配置信息
			// state.sysBaseConfig = {}
			// storage.remove(constant.sysBaseConfig)
			// storage.clean()
		},
	},
	actions: {
		// 登录获取token
		Login({
			commit,
			state
		}, userInfo) {
			const paramData = {
				username: userInfo.account.trim(),
				// 密码进行SM2加密，传输过程中看到的只有密文，后端存储使用hash
				password: smCrypto.doSm2Encrypt(userInfo.password),//先不加密,后面再改成加密的形式
				captcha: userInfo.validCode,
				checkKey: 1629428467008 //new Date().getTime();TODO 兼容mock和接口，暂时这样处理
			}
			return new Promise((resolve, reject) => {
				loginApi.login(paramData).then(data => {
					// 缓存token
					 const { token } = data
					commit('SET_token', token)
					resolve(data)
				}).catch(error => {
					reject(error)
				})
			})
		},
		// 获取用户信息
		GetUserInfo({
			commit,
			state
		},userinfo) {
			console.log("userinfo:",userinfo);
			// 缓存用户信息
			commit('SET_userInfo', userinfo)
		},
		// 获取登錄用戶菜單
		GetUserLoginMenu({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				userCenterApi.userLoginMobileMenu().then(data => {
					// 缓存移动端用户菜单
					let {wechatMenus} = data;
					commit('SET_userMobileMenus', wechatMenus)
					resolve(wechatMenus)
				}).catch(error => {
					reject(error)
				})
			})
		},
		
		
		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				loginApi.logout().then(() => {	
					// 清除缓存
					commit('CLEAR_cache')
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}